package com.hksj.vsm.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hksj.common.core.enums.DeviceState;
import com.hksj.common.core.exception.ApplicationException;
import com.hksj.vsm.dto.VideoDeviceDTO;
import com.hksj.vsm.dto.VideoDeviceStateDTO;
import com.hksj.vsm.entity.VideoDevice;
import com.hksj.vsm.mapper.VideoDeviceManageMapper;
import com.hksj.vsm.query.VideoDeviceQuery;
import com.hksj.vsm.vo.VideoDeviceVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 视频设备 服务类
 * @author chenxingqing
 */
@Service
public class VideoDeviceManageService extends ServiceImpl<VideoDeviceManageMapper, VideoDevice> {

    public VideoDevice addVideoDevice(VideoDeviceDTO dto) {
        VideoDevice videoDevice = new VideoDevice();
        BeanUtils.copyProperties(dto,videoDevice);
        videoDevice.setState(DeviceState.notActive.getValue());
        videoDevice.setCreateTime(new Date());
        videoDevice.setModifyTime(new Date());
        videoDevice.setCreatorId(dto.getUserId());
        videoDevice.setCreatorName(dto.getUname());
        videoDevice.setModifierId(dto.getUserId());
        videoDevice.setModifierName(dto.getUname());
        baseMapper.insert(videoDevice);
        return videoDevice;
    }

    public VideoDeviceVO getVideoDevice(String id) {
        return baseMapper.getVideoDevice(id);
    }

    public void updateVideoDevice(VideoDeviceDTO dto) {
        VideoDevice videoDevice = new VideoDevice();
        BeanUtils.copyProperties(dto,videoDevice);
        videoDevice.setModifyTime(new Date());
        videoDevice.setModifierId(dto.getUserId());
        videoDevice.setModifierName(dto.getUname());
        baseMapper.updateById(videoDevice);
    }

    public void delVideoDevice(String id) {
        VideoDeviceVO videoDevice = getVideoDevice(id);
        if (!DeviceState.notActive.getValue().equals(videoDevice.getState())){
            throw new ApplicationException("启用中的设备无法删除");
        }
        baseMapper.deleteById(id);
    }

    public IPage<VideoDeviceVO> pageVideoDevice(VideoDeviceQuery query) {
        Page<VideoDeviceVO> page = new Page<>(query.getPageNum(),query.getPageSize());
        List<VideoDeviceVO> list = baseMapper.pageVideoDevice(query,query.getDisAblePage() ? null : page);
        page.setRecords(list);
        return page;
    }

    public void enableVideoDevice(VideoDeviceStateDTO dto) {
        VideoDevice videoDevice = new VideoDevice();
        videoDevice.setModifyTime(new Date());
        videoDevice.setModifierId(dto.getUserId());
        videoDevice.setModifierName(dto.getUname());
        videoDevice.setId(dto.getId());
        if (dto.getEnable() == 1){
            videoDevice.setState(DeviceState.offline.getValue());
        }else if (dto.getEnable() == 0){
            videoDevice.setState(DeviceState.notActive.getValue());
        }
        baseMapper.updateById(videoDevice);
    }


    /**
     * 分页查询视频设备实体列表
     * @param query 设备设备查询对象
     * @return 分页结果
     */
    public IPage<VideoDeviceVO> queryVideoDeviceInstanceListByPaging(VideoDeviceQuery query){

        //创建一个分页实例
        IPage<VideoDeviceVO> page = new Page<>(query.getPageNum(),query.getPageSize());

        //调用baseMapper的queryVideoDeviceListByPaging方法分页查询
        List<VideoDeviceVO> videoDeviceList = baseMapper.queryVideoDeviceInstanceListByPaging(page, query);

        //将查询结果设置到分页对象
        page.setRecords(videoDeviceList);

        //返回分页结果
        return page;
    }
}
